package com.my.storage.mapper;

import com.my.storage.model.FileMetadata;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author xhw
 */

@Mapper
public interface FileMapper {
    // 插入文件元数据
    @Insert("INSERT INTO file_metadata (path, depth, size, upload_time, type, is_deleted) " +
            "VALUES (#{path}, #{depth}, #{size}, #{uploadTime}, #{type}, #{isDeleted})")
    void insert(FileMetadata fileMetadata);

    // 更新文件元数据
    @Update("UPDATE file_metadata SET size = #{size}, upload_time = #{uploadTime}, type = #{type} " +
            "WHERE path = #{path} AND is_deleted = false")
    void update(FileMetadata fileMetadata);

    // 判断路径是否存在
    @Select("SELECT EXISTS (SELECT 1 FROM file_metadata WHERE path = #{path} AND is_deleted = false)")
    boolean existsPath(@Param("path") String path);

    // 查询根目录下的文件和文件夹
    @Select("SELECT * FROM file_metadata WHERE depth = 1 AND is_deleted = false")
    List<FileMetadata> findRootFiles();

    // 根据路径和深度查询用户目录下的文件和文件夹
    @Select("SELECT * FROM file_metadata WHERE  path LIKE #{logicPath} AND depth " +
            "= #{depth} AND is_deleted = false")
    List<FileMetadata> findFilesByPathAndDepth(@Param("logicPath") String logicPath,
                                               @Param("depth") int depth);

    //通过用路径查找文件信息
    @Select("SELECT * FROM file_metadata WHERE path = #{path} AND is_deleted = false")
    FileMetadata findByPath(@Param("path") String path);

    //按照完整路径删除单个文件或目录
    @Update("UPDATE file_metadata SET is_deleted = TRUE WHERE path = #{path}")
    void deleteByPath(@Param("path") String path);

}